package com.lxk.service;

import com.lxk.bean.TableInfo;
import com.lxk.builder.*;
import com.lxk.mapper.TableInfoMapper;
import com.lxk.bean.Constants;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.util.List;

@Service
public class CodeGeneratorService implements CodeGeneratorServiceImpl{
    /**
     * 获取表信息（动态连接）
     */
    @Override
    public List<TableInfo> getTables(Connection conn) {

        TableInfoMapper tableInfoMapper = new TableInfoMapper();
        return tableInfoMapper.getTables(conn);
    }

    /**
     * 生成代码（动态连接）
     */
    @Override
    public void generateCode(Connection conn, String outputPath) {
        //设置项目路径
        if(outputPath != null && !outputPath.isEmpty()) {
            outputPath = outputPath+"/src/main/";
            Constants.setBasePath(outputPath);
        }
//        System.out.println("shuru: "+Constants.PATH_BASE);
//        System.out.println("shuru: "+Constants.PATH_PO);

        TableInfoMapper tableInfoMapper = new TableInfoMapper();
        List<TableInfo> tableInfoList = tableInfoMapper.getTables(conn);

        BuildBase.execute();

        for (TableInfo tableInfo : tableInfoList) {
            BuildPo.execute(tableInfo);
            BuildQuery.execute(tableInfo);
            BuildMapper.execute(tableInfo);
            BuildMapperXml.execute(tableInfo);
            BuildService.execute(tableInfo);
            BuildServiceImpl.execute(tableInfo);
            BuildController.execute(tableInfo);
        }
    }
}
